package fatworm.concept;

/**
 *	field in the schema
 */
public class DataField {
	/** the name of the field */
	private	String name;
	/** the data type of the field */
	private DataType	type;
	/** the default value of the field */
	private DataType.SqlValue	defaultValue;
	/** whether the field is not null */
	private boolean notNull = false;
	/** whether the field is unique */
	private boolean primaryKey = false;
	/** whether the field is auto increment */
	private boolean autoIncrement = false;
	
	public DataField( String name , DataType type ){
		this( name , type , DataType.getNullValue() );
	}
	
	public DataField( String name , DataType type , 
			DataType.SqlValue defaultValue  ){
		this.name = name; this.type = type; 
		this.defaultValue = defaultValue;
	}
	
	public String toString(){
		String ans = name +" "+type ;
		if( this.isNotNull() ) ans += " not null";
		if( !this.getDefaultValue().isNull() ) 
			ans += " default "+ this.getDefaultValue();
		if( this.isPrimaryKey() ) ans +=" primary key";
		if( this.isAutoIncrement() ) ans += " auto_increment";
		return ans;
	}
	/**
	 * @return whether the field is auto increment 
	 */
	public boolean isAutoIncrement(){
		return this.autoIncrement;
	}
	/**
	 * @return whether the field is not null
	 */
	public boolean isNotNull(){
		return this.notNull;
	}
	
	/**
	 * @return whether the field is primary key
	 */
	public boolean isPrimaryKey(){
		return this.primaryKey;
	}
	/**
	 * @return the default value of the field
	 */
	public DataType.SqlValue getDefaultValue(){
		return defaultValue;
	}
	/**
	 * set this field to be primary key
	 */
	public void setPrimaryKey(){
		this.notNull = true; 
		this.primaryKey = true; 
 	}
	/**
	 * set this field to be not null
	 */
	public void setNotNull(){
		this.notNull = true;
	}
	/**
	 * set this field to be autoIncrement
	 */
	public void setAutoIncrement(){
		this.autoIncrement = true;
	}
	/**
	 * set the default value of the field
	 * @param value the default value
	 */
	public void setDefaultValue( DataType.SqlValue value ){
		this.defaultValue = value;
	}
	/**
	 * @return the field's name
	 */
	public String getName(){
		return name;
	}
	/**
	 * @return the data type of the field
	 */
	public DataType getType(){
		return type;
	}
}
